package com.permission.controller;

import com.permission.model.PerDept;
import com.permission.model.PerUser;
import com.permission.service.PerDeptService;
import com.permission.service.PerUserService;
import com.solution.framework.controller.BaseController;
import com.solution.framework.util.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with generator tools.
 * User: sunguangzhu
 * DateTime:2014-04-28 04:08:30
 */
@Controller
public class PerUserController extends BaseController<PerUser> {

    private static final String path_index = "perUser/perUser_index";
    private static final String path_input = "perUser/perUser_input";
    private static final String path_view = "perUser/perUser_view";
    private static final String path_search = "perUser/perUser_search";

    @Resource
    private PerUserService perUserService;

    @Resource
    private PerDeptService perDeptService;

    @RequestMapping
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("index method ....");
        return path_index;
    }

    @Override
    protected List<Criterion> getSearchCriterions(HttpServletRequest request) {
        //用hibernate提供的类
        //simple search
        String search_type = RequestVarsUtil.getString(request, "search_type");
        List<Criterion> criterions = new ArrayList<Criterion>();
        if (StringUtils.isNotBlank(search_type)) {
            if (search_type.equals(Constant.SIMPLE_SEARCH_TYPE)) {
                String param = RequestVarsUtil.getString(request, "param");
                String value = RequestVarsUtil.getString(request, "value");
                if (StringUtils.isNotBlank(value)) {
                    if (param.equals("all")) {
                        //模糊查询所有字段
                        criterions.add(Restrictions.or(Restrictions.like("propStr", "%" + value + "%"), Restrictions.like("description", "%" + value + "%")));
                    } else {
                        //查询指定字段
                        criterions.add(Restrictions.like(param, "%" + value + "%"));
                    }
                }
            } else {
                //高级查询
                String name = RequestVarsUtil.getString(request, "name");
                criterions.add(Restrictions.eq("name", name));
            }
        }

        //other search
        Long nodeId = RequestVarsUtil.getLong(request, "nodeId");
        if (nodeId.longValue() != 0l) {
            criterions.add(Restrictions.eq("perDept.id", nodeId));
        }
        return criterions;    //To change body of overridden methods use File | Settings | File Templates.
    }

    @ResponseBody
    @RequestMapping
    protected void getGridData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Pagination pagination = RequestVarsUtil.getPagination(request);
        List<Criterion> criterions = this.getSearchCriterions(request);
        List<PerUser> entitys = this.perUserService.listByCriteria(pagination, criterions);
        int total = this.perUserService.getTotalByCriteria(criterions);
        ResultDto<List<PerUser>> resultDto = new ResultDto<List<PerUser>>(entitys, total);
        System.out.println(" resultDto.toString() = " + resultDto.toString());
        super.writeToClient(response, resultDto.toString());
    }

    @RequestMapping
    public String edit(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("edit method ....");
        PerUser perUser = this.perUserService.get(id);
        model.addAttribute("perUser", perUser);
        return path_input;
    }

    @RequestMapping
    public String add(Long bizId, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("add method ....");
        PerUser perUser = new PerUser();
        PerDept perDept = null;
        if (bizId != 0l) {
            perDept = this.perDeptService.get(bizId);
        } else {
            perDept = new PerDept();
            perDept.setId(0l);
        }
        perUser.setPerDept(perDept);
        model.addAttribute("perUser", perUser);
        return path_input;
    }

    @RequestMapping
    public String view(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("view method ....");
        PerUser perUser = this.perUserService.get(id);
        model.addAttribute("perUser", perUser);
        return path_view;
    }

    @RequestMapping
    public String search(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("search method ....");
        return path_search;
    }

    @ResponseBody
    @RequestMapping
    @DateTimeFormat
    public void save(PerUser perUser, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("save method ....");
        perUser.setPassword(MD5Utils.encrype("123456"));
        MessageDto messageDto = new MessageDto();
        try {
            if (perUser.getId() != null) {
                this.perUserService.update(perUser);
                super.setMessageDto(messageDto, MessageDefined.update_success_info);
            } else {
                this.perUserService.save(perUser);
                super.setMessageDto(messageDto, MessageDefined.save_success_info);
            }
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            e.printStackTrace();
        }
        super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void delete(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        MessageDto messageDto = new MessageDto();
        try {
            Integer count = this.perUserService.delete(ids);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            e.printStackTrace();
        }
        super.writeToClient(response, messageDto.toString());
    }
}
